﻿using Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using Utility.Helper;
using YQHL.GS.Model;

namespace BLL
{
    /// <summary>
    /// 任务调动BLL
    /// </summary>
    public class YX_BANK_TASK_BLL : BaseBLL
    {
        /// <summary>
        /// 查询单条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public YX_BANK_TASK Find(string id)
        {
            var sql = @"SELELCT T1.* FROM YX_BANK_TASK T1 WHERE T1.ID = :ID";
            var parms = new List<Tuple<string, string, string>>();
            parms.Add(":ID", id, "string");
            var dt = Query(sql, parms);
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            var row = dt.Rows[0];
            var entity = new YX_BANK_TASK();
            entity.ID = row.GetValue<string>("ID");
            entity.NAME = row.GetValue<string>("NAME");
            entity.BEGIN_DATE = row.GetValue<DateTime?>("BEGIN_DATE");
            entity.END_DATE = row.GetValue<DateTime?>("END_DATE");
            entity.STATE = row.GetValue<int>("STATE");
            entity.REMARK = row.GetValue<string>("REMARK");
            entity.TASK_CODE = row.GetValue<string>("TASK_CODE");
            entity.APPLY_DATE = row.GetValue<DateTime?>("APPLY_DATE");
            return entity;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        public void Insert(YX_BANK_TASK entity)
        {
            entity.ID = Guid.NewGuid().ToString();
            var sql = @"INSERT INTO YX_BANK_TASK(
                            ID,NAME,BEGIN_DATE,
                            END_DATE,STATE,
                            REMARK,TASK_CODE,
                            APPLY_DATE)
                        VALUES(
                            :ID,:NAME,:BEGIN_DATE,
                            :END_DATE,:STATE,
                            :REMARK,:TASK_CODE,
                            :APPLY_DATE)";
            var parms = new List<Tuple<string, string, string>>();
            parms.Add(":ID", entity.ID, "string");
            parms.Add(":NAME", entity.NAME, "string");
            parms.Add(":BEGIN_DATE", entity.BEGIN_DATE, "datetime");
            parms.Add(":END_DATE", entity.END_DATE, "datetime");
            parms.Add(":STATE", entity.STATE, "int");
            parms.Add(":REMARK", entity.REMARK, "string");
            parms.Add(":TASK_CODE", entity.TASK_CODE, "string");
            parms.Add(":APPLY_DATE", entity.APPLY_DATE, "datetime");

            NoQuery(sql, parms);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        public void Update(YX_BANK_TASK entity)
        {
            entity.ID = Guid.NewGuid().ToString();
            var sql = @"UPDATE YX_BANK_TASK T1 SET
                            T1.NAME = :NAME,
                            T1.BEGIN_DATE = :BEGIN_DATE,
                            T1.END_DATE = :END_DATE,
                            T1.STATE = :STATE,
                            T1.REMARK = :REMARK,
                            T1.TASK_CODE = :TASK_CODE,
                            T1.APPLY_DATE = :APPLY_DATE
                        WHERE
                            T1.ID = :ID";
            var parms = new List<Tuple<string, string, string>>();
            parms.Add(":ID", entity.ID, "string");
            parms.Add(":NAME", entity.NAME, "string");
            parms.Add(":BEGIN_DATE", entity.BEGIN_DATE, "datetime");
            parms.Add(":END_DATE", entity.END_DATE, "datetime");
            parms.Add(":STATE", entity.STATE, "int");
            parms.Add(":REMARK", entity.REMARK, "string");
            parms.Add(":TASK_CODE", entity.TASK_CODE, "string");
            parms.Add(":APPLY_DATE", entity.APPLY_DATE, "datetime");
            NoQuery(sql, parms);
        }

        /// <summary>
        /// 获取一条工行任务
        /// </summary>
        /// <returns></returns>
        public void GetEntityByYQHL_GS(out PageQuery query, out Action<JArray, DateTime> successAciton, out Action<Exception> erroAction)
        {
            query = null;
            successAciton = null;
            erroAction = null;
            var sql = @"SELECT * FROM (SELECT
                                          rownum R,
                                          T1.ID,
                                          T2.Account_No AS ACCOUNT_NUMBER,
                                          T3.ID AS YQHL_ID,
                                          T3.BEIN_DATE,
                                          T3.END_DATE
                                        FROM YX_BANK_TASK T1
                                        INNER JOIN YX_ACCOUNT T2 ON T1.TASK_CODE = 'YQHL-GH-'||TO_CHAR(T2.COMPANY_ID)
                                        INNER JOIN YX_TASK_YQHL T3 ON T1.TASK_ID = ID
                                        WHERE
                                              T1.STATE = 0
                                        ORDER BY T1.APPLY_DATE
                                        ) T1
                        WHERE
                            T1.R =1";
            var dt = Query(sql);
            if (dt.Rows.Count == 0)
            {
                return;
            }
            sql = @"UPDATE YX_BANK_TASK T1
                    SET
                        T1.STATE = 2
                    WHERE
                        T1.ID = :ID";
            var parms = new List<Tuple<string, string, string>>();

            var row = dt.Rows[0];
            parms.Add(":ID", row.GetValue<string>("ID"), "string");
            NoQuery(sql, parms);
            query = new PageQuery();
            query.AccNo = row.GetValue<string>("ACCOUNT_NUMBER");
            query.BeginDate = row.GetValue<DateTime>("BEIN_DATE");
            query.EndDate = row.GetValue<DateTime>("END_DATE");
            query.MaxAmt = 1 / 100;
            query.MaxAmt = 999999999999 / 100;
            query.NextTag = "";
            query.ReqReserved1 = "";
            query.ReqReserved2 = "";
            successAciton = (JArray arry, DateTime successDt) =>
            {
                using (var conn = GetDbConn())
                {
                    conn.Open();
                    sql = @"UPDATE YX_BANK_TASK T1
                    SET
                        T1.STATE = 1,
                        T1.END_DATE = :END_DATE
                    WHERE
                        T1.ID = :ID";
                    parms.Clear();
                    parms.Add(":ID", row.GetValue<string>("ID"), "string");
                    parms.Add(":END_DATE", successDt, "datetime");
                    NoQuery(sql, parms);
                    //检测流水是否已经插入
                    //var sql = "INSERT INTO";
                }
            };
            erroAction = (Exception ex) =>
            {
                //记录日志
                sql = @"UPDATE YX_BANK_TASK T1
                    SET
                        T1.STATE = 3,
                        T1.REMARK = :REMARK
                    WHERE
                        T1.ID = :ID";
                parms.Clear();
                parms.Add(":ID", row.GetValue<string>("ID"), "string");
                parms.Add(":REMARK", ex.Message, "string");
                NoQuery(sql, parms);
            };
        }
    }
}